package com.rp.user.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.rp.bo.UpdateUserInfoBO;
import com.rp.news.common.NewsCommon;
import com.rp.news.common.RedisCommon;
import com.rp.news.common.ResponseStatusEnum;
import com.rp.news.enums.Sex;
import com.rp.news.enums.UserStatus;
import com.rp.news.exception.GraceException;
import com.rp.news.util.DesensitizationUtil;
import com.rp.news.util.RedisOperator;
import com.rp.pojo.AppUser;
import com.rp.user.mapper.AppUserMapper;
import com.rp.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotBlank;
import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private RedisOperator redisOperator;

    @Autowired
    private AppUserMapper appUserMapper;

    @Override
    public AppUser queryIsExistByMobile(String mobile) {

        Example example = new Example(AppUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("mobile", mobile);
        AppUser appUser = appUserMapper.selectOneByExample(example);
        return appUser;
    }


    @Override
    public AppUser createUser(String mobile) {
        AppUser appUser = new AppUser();
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        String idStr = snowflake.nextIdStr();

        appUser.setId(idStr);
        appUser.setMobile(mobile);
        appUser.setNickname("用户:" + DesensitizationUtil.commonDisplay(mobile));
        appUser.setFace(NewsCommon.USER_FACE0);
        appUser.setSex(Sex.secret.type);
        appUser.setBirthday(DateUtil.parse("1990-01-01"));
        appUser.setActiveStatus(UserStatus.INACTIVE.type);
        appUser.setTotalIncome(0);
        appUser.setCreatedTime(new Date());
        appUser.setUpdatedTime(new Date());
        int insert = appUserMapper.insert(appUser);
        return appUser;
    }

    @Override
    public AppUser getUser(String userId) {
        AppUser appUser = appUserMapper.selectByPrimaryKey(userId);
        return appUser;
    }

    @Override
    public void updateUserInfo(UpdateUserInfoBO updateUserInfoBO) {
        String userId = updateUserInfoBO.getId();
        redisOperator.del(RedisCommon.REDIS_USER_INFO + ":" + userId);
        AppUser appUser = Convert.convert(AppUser.class, updateUserInfoBO);
        appUser.setUpdatedTime(new Date());
        appUser.setActiveStatus(UserStatus.ACTIVE.type);
        int i = appUserMapper.updateByPrimaryKeySelective(appUser);
        if(i != 1){
            GraceException.display(ResponseStatusEnum.USER_UPDATE_ERROR);
        }
        AppUser user = getUser(appUser.getId());
        redisOperator.set(RedisCommon.REDIS_USER_INFO + ":" + userId, JSONUtil.toJsonStr(user));
    }

}
